package org.wyn.interceptor;

import javax.servlet.http.HttpServletRequest;

import org.apache.struts2.StrutsStatics;
import org.wyn.exception.BGException;
import org.wyn.exception.LoginException;
import org.wyn.mongodb.dao.impl.UserDao;
import org.wyn.util.UserCookie;

import com.google.inject.Inject;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.interceptor.AbstractInterceptor;

public class InterceptorAction extends AbstractInterceptor{
	@Override
	public String intercept(ActionInvocation invocation) throws BGException, LoginException, Exception {
		try {		
			ActionContext actionContext = invocation.getInvocationContext();
			HttpServletRequest request = (HttpServletRequest) actionContext.get(StrutsStatics.HTTP_REQUEST);
			
			if(UserCookie.isLoginStatus(request)){
				String userId = UserCookie.getUserId(request);
				String userPass = UserCookie.getUserPassword(request);
				if(dao.isUserIn(userId, userPass)){
					throw new LoginException("unlogin");
				}
			}else{
				throw new LoginException("unlogin");
			}
		} catch (Exception e) {
			throw new LoginException("unlogin");
		}
		return invocation.invoke();
	}

	@Inject
	private UserDao dao;

	public UserDao getDao() {
		return dao;
	}

	public void setDao(UserDao dao) {
		this.dao = dao;
	}
}
